import groovy.json.JsonOutput;

def getTriggeredUser() {

    def user = params.TRIGGERD_USER

    if (user) {
        return user
    }

    wrap([$class: 'BuildUser']) {
        user = env.BUILD_USER
    }

    return user
}

def clone(repo, branch) {
    def credentialsId = 'wweike-gitlab-jenkins-token'

    checkout([$class: 'GitSCM',
              branches: [[name: branch]],
              doGenerateSubmoduleConfigurations: false,
              extensions: [[$class: 'CloneOption', depth: 0, shallow: true, noTags: false],
                           [$class: 'SubmoduleOption',
                            disableSubmodules: false,
                            parentCredentials: true,
                            recursiveSubmodules: true,
                            reference: '',
                            trackingSubmodules: false]],
              submoduleCfg: [],
              userRemoteConfigs: [[credentialsId: credentialsId,
                                   url: "https://gitlab.weike.fm/${repo}"]]])
}

def cleanWorkspace() {
    // clean workspace
    sh 'sudo git clean -fdx'
    deleteDir()
}

def cleanDockerImage(image) {
    sh "docker image rm ${image} || true"
}

def judgeClusterByEnv(env, prodCluster) {
    String cluster

    switch (env) {
        case "dev":
            cluster = "dev"
            break
        case "stage":
            cluster = "wegton"
            break
        case "prod":
            cluster = prodCluster
            break
        default:
            throw new IllegalArgumentException("Unknown environment: ${env}")
            break
    }
    return cluster
}


def judgeNamespaceByEnv(env, team, defaultNamespace) {
    String namespace
    if (env == "prod" || env == "stage") {
        if (team == "wegton" || team == "longsee" || team == "doujia") {
            namespace = "default"
        } else {
            namespace = defaultNamespace
        }
    } else {
        namespace = defaultNamespace
    }
    return namespace
}


def setLongseeRegistry() {
    setRegistryConfig("longsee")
}

def setWegtonRegistry() {
    setRegistryConfig("wegton")
}

def setBBGRegistry() {
    setRegistryConfig("bbg")
}

def setNuwaAliyunRegistry() {
    setRegistryConfig("nuwa-aliyun")
}

def setRegistryConfig(clusterName) {
    sh "cp /var/lib/jenkins/.docker/config-${clusterName}.json /var/lib/jenkins/.docker/config.json"
}

def getEnvConfigHost(env) {
    if (env == "stage") {
       return "http://106.55.80.164:8080"
    } else if (env == "prod") {
        return "http://81.71.86.102:8080"
    } else if (env == "dev") {
        return "http://81.71.145.204:8080"
    } else {
        throw new IllegalArgumentException("Unknown environment: ${env}")
    }
}

def pushImageToLongseeRegistry(imageTag) {
    setLongseeRegistry()
    sh "docker push ${imageTag}"
}

def pushImageToWegtonRegistry(imageTag) {
    setWegtonRegistry()
    sh "docker push ${imageTag}"
}

def pushImageToBBGRegistry(imageTag) {
    setBBGRegistry()
    sh "docker push ${imageTag}"
}

def pushImageToNuwaAliyunRegistry(imageTag) {
    setNuwaAliyunRegistry()
    sh "docker push ${imageTag}"
}

return this
